package secondStage.part3.List集合.LinkedList集合三种遍历运用;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;

/**
 * @ClassName: [LinkedListTest]
 * @Description: LinkedList集合存储学生对象遍历
 * @Author: [黄劲松]
 * @CreateDate: [2021-04-03]
 * @Version: [v1.0]
 */
//需求:创建一个存储学生对象的集合。存储3个学生对象，使用程序实现在控制台遍历该集合
public class LinkedListTest {
    public static void main(String[] args) {
        //创建LinkedList对象
        LinkedList<Student> array = new LinkedList<Student>();
        //创建学生对象
        Student s1 = new Student("张三", 12);
        Student s2 = new Student("李四", 12);
        Student s3 = new Student("王五", 12);
        array.add(s1);
        array.add(s2);
        array.add(s3);

        //迭代器，集合的特有的遍历方式
        Iterator<Student> it = array.iterator();
        while (it.hasNext()) {
            Student s = it.next();
            System.out.println(s.getName() + "," + s.getAge());
        }
        //普通for循环，带有索引的遍历方式
        for (int i = 0; i < array.size(); i++) {
            Student s = array.get(i);
            System.out.println(s.getName() + ":" + s.getAge());
        }
        //增强for，最为方便的循环遍历方式
        for (Student s : array) {
            System.out.println(s.getName() + "--->" + s.getAge());
        }
    }
}
